package com.zx.club.oss.adapter;

import com.zx.club.oss.config.MinioProperties;
import com.zx.club.oss.config.OssProperties;
import com.zx.club.oss.util.MinioUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 
 * @author zx
 * @date 2025-03-20 09:14
 */
public class MinioStorageServiceAdapter implements StorageServiceAdapter{

    private final OssProperties ossProperties;
    private final MinioUtil minioUtil;
    public MinioStorageServiceAdapter(OssProperties ossProperties, MinioUtil minioUtil) {
        this.ossProperties = ossProperties;
        this.minioUtil = minioUtil;
    }

    @Override
    public String uploadFile(MultipartFile file) {
        try {
            MinioProperties minio = ossProperties.getMinio();
            String bucketName = minio.getBucketName();
            String objectName = file.getOriginalFilename();
            minioUtil.uploadFile(file.getInputStream(), bucketName,objectName);
            String imgUrl = minio.getUrl()
                    + "/"
                    + bucketName
                    + "/"
                    + file.getOriginalFilename();


            return imgUrl;
        } catch (Exception e) {
            log.error("上传文件失败,失败原因:{}",e.getMessage(), e);
            e.printStackTrace();
        }
        return null;
    }


}
